var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		bottom:0,
		pagenum: 1,
		sort:'',
		field:'',
		clist: [],
		curIndex: -1,
		curIndex2: -1,
		prolist: [],
		nopro: 0,
		curCid: 0,
		buydialogHidden: true,
		loadingHidden: false,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		prodata: [],
		userinfo: [],
		isload: 0
	},
	_onLoad: function (options) {
		var that = this;
		var nowcid = that.options.cid
		if(!nowcid) nowcid = '';
		that.setData({ pagenum: 1, prolist: [], $nomoreData: false });
		$.get('business/classify', {cid: nowcid,bid:that.options.bid}, function (res) {
			var data = res.clist
			that.setData({
				clist: data,
				prolist:res.datalist,
				curCid: data[0]['id']
			});
			if (nowcid) {
				for (var i = 0; i < data.length; i++) {
					if (data[i]['id'] == nowcid) {
						that.setData({
							curIndex: i,
							curCid: nowcid
						});
						break;
					}
					var downcdata = data[i]['child'];
					var isget = 0
					for (var j = 0; j < downcdata; j++) {
						if (downcdata[j]['id'] == nowcid) {
							that.setData({
								curIndex: i,
								curIndex2: j,
								curCid: nowcid
							});
							isget = 1
							break;
						}
					}
					if (isget) break;
				}
			}
			//that.getprolist();
		});
	},
	onReady:function(){
		var that = this;
	},
	onPullDownRefresh: function () {
		this.onLoad();
		wx.stopPullDownRefresh()
	},
	onReachBottom:function(){
		console.log('11')	
	},
	scrolltolower:function(){
		if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
			this.setData({ pagenum: this.data.pagenum + 1 })
			this.getprolist();
		}
	},
	getprolist: function () {
		var that = this
		var pagenum = that.data.pagenum
		var cid = that.data.curCid;
		var order = that.data.order
		var field = that.data.field
		$.post('business/classify', { pagenum: pagenum, field: field, order: order, cid: cid,bid:that.options.bid}, function (res) {
			var data = res.data
			if (pagenum == 1) {
				that.setData({ prolist: data });
				if (data.length == 0) {
					$.showEmpty('没有相关商品','/images/empty2.png');
				}
			} else {
				if (data.length == 0) {
					//$.error('没有更多数据了');
					that.setData({ $nomoreData: true });
				} else {
					var prolist = that.data.prolist
					var newdata = prolist.concat(data)
					that.setData({ prolist: newdata });
				}
			}
		});
	},
	//改变子分类
	changeCTab: function (e) {
		var that = this
		var id = e.target.dataset.id;
		var index = parseInt(e.target.dataset.index);
		this.setData({
			curIndex2: index,
			prolist: [],
			nopro: 0,
			curCid: id
		});
		that.setData({
			pagenum: 1,
			prolist: [],
			$nomoreData: false
		});
		this.getprolist();
	},
	//改变排序规则
	changeOrder: function (e) {
		var that = this;
		var t = e.currentTarget.dataset;
		that.setData(t);
		that.setData({
			pagenum: 1,
			prolist: [],
			$nomoreData: false
		});
		this.getprolist();
	},

	//事件处理函数
	switchRightTab: function (e) {
		var that = this
		var id = e.target.dataset.id;
		var index = parseInt(e.target.dataset.index);
		this.setData({
			curIndex: index,
			curIndex2: -1,
			prolist: [],
			nopro: 0,
			curCid: id
		});
		that.setData({
			pagenum: 1,
			prolist: [],
			$nomoreData: false
		});
		this.getprolist();
	},
}))